<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>地市福利编辑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="/plugins/layui/css/layui.css" media="all" />
    <style type="text/css">
        .layui-form-item .layui-inline {
            width: 33.333%;
            float: left;
            margin-right: 0;
        }

        @media (max-width: 1240px) {
            .layui-form-item .layui-inline {
                width: 100%;
                float: none;
            }
        }
    </style>
</head>
<body class="childrenBody">
	<form class="layui-form">
	
		  <div class="layui-form-item">
			    <label class="layui-form-label">所属区域</label>
			    <div class="layui-input-block">
			      <select name="regionId" lay-verify="required" id="regionId">
			      
			      </select>
			    </div>
	  	</div>
		<div class="layui-form-item">
			<label class="layui-form-label">福利名称</label>
			<div class="layui-input-block">
				<input type="text" class="layui-input username" lay-verify="required" name="welfareName" placeholder="请输入福利名称">
			</div>
		</div>
		
		<div class="layui-form-item">
			<label class="layui-form-label">福利价格</label>
			<div class="layui-input-block">
				<input type="text" class="layui-input newsName"  name="welfarePrice" lay-verify="required||number" placeholder="请输入福利价格">
			</div>
		</div>
		<div class="layui-form-item">
			<div class="layui-input-block">
				<button class="layui-btn" lay-submit="" lay-filter="editWelfares">立即提交</button>
				<button type="reset" class="layui-btn layui-btn-primary">重置</button>
		    </div>
		</div>
	</form>

<!--一般情况下，一个业务页面引入一个layui.js和业务js这两个两个js就够了-->
<!-- <script type="text/javascript" src="../../../lib/layui/layui.js"></script> -->
<script type="text/javascript" src="/js/jquery-1.11.2.min.js"></script>
<script type="text/javascript" src="/plugins/layui/layui.js"></script>
<!-- <script type="text/javascript" src="editMenu.js"></script> -->
<script type="text/javascript">
function renderForm(){
	  layui.use('form', function(){
	   var form = layui.form;//高版本建议把括号去掉，有的低版本，需要加()
	   form.render();
	  });
	 }
var regionsList;
$.ajax({
    type : "GET",
    url: '/regions/getAll',
    dataType:'json',
 	  async:false,
    success: function(data){
    	var list=data.data;
    	regionsList=list;
    	var html='';
        for(var i=0;i<list.length;i++){	
            html += "<option value='"+list[i].id+"'>"+list[i].regionName+"</option>"
        }
        $("#regionId").append(html);
        renderForm();
    }
});

layui.config({
    base: '/js/admin/'
}).extend({
    ajaxExtention: 'ajaxExtention',//加载自定义扩展，每个业务js都需要加载这个ajax扩展
    $tool: 'tool'
}).use(['form', 'layer', 'tree', 'jquery', 'ajaxExtention', '$tool'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : parent.layer,
        $ = layui.jquery,
        $tool = layui.$tool;
    

     initUserInfo();
    /**
     * 初始化用户信息
     * */
     function initUserInfo() {
        var queryArgs = $tool.getQueryParam();//获取查询参数
        var id = queryArgs['id'];
        var url = '/welfares/getById/'+id;
        $.ajax({
            url:url,
            method:"GET",
            success:function (data) {
                var datas = data.data;
                $("input[name='welfareName']").val(datas.welfareName);
                $("input[name='welfarePrice']").val(datas.welfarePrice);
                $("select option[value='"+datas.regionId+"']").attr("selected","selected"); 
                form.render();//重新绘制表单，让修改生效
            }
         
        });
    }
  
    /**
     * 表单提交
     * */
     form.on("submit(editWelfares)", function (data) {
        var queryArgs = $tool.getQueryParam();//获取查询参数
        var id = queryArgs['id'];
        var welfareName = data.field.welfareName;
        var welfarePrice = data.field.welfarePrice;
        var regionId = data.field.regionId;
       

        //请求
        var url ='/welfares';
        var req = {
            id:id,
            welfareName: welfareName,
            welfarePrice: welfarePrice,
            regionId: regionId,
        };
        $.ajax({
            url: url,
            contentType:"application/json;charset=UTF-8",
            data: JSON.stringify(req),
            method: "PUT",
            success: function (data) {
                //top.layer.close(index);(关闭遮罩已经放在了ajaxExtention里面了)
                if(data.code==2000){
                	 layer.msg("地市福利更新成功！", {time: 1000}, function () {
                         layer.closeAll("iframe");
                         //刷新父页面
                         parent.location.reload();
                     });
                }
               
            }
           
        });

        return false;
    }) 

});

</script>
</body>
</html>

